import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver } from '@angular/core';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
import { AppPaginationComponent, PaginationModel } from "../../../../../widgets/pagination/pagination.component";
import { environment } from "../../../../../../environments/environment";
import * as _ from "lodash";
import { InputModeComponent } from '../../content/input-mode/input-mode.component';
import { RouteModeComponent } from '../../content/route-mode/route-mode.component';
import { BatchModeComponent } from '../../content/batch-mode/batch-mode.component';
import { RemindComponent } from '../../content/remind/remind.component';
import { RecordService } from './record.service';
@Component({
  selector: 'app-record',
  templateUrl: './record.component.html',
  styleUrls: ['./record.component.scss'],
  providers: [RecordService]
})
export class RecordComponent implements OnInit {
  // 录入模式
  @ViewChild("inputMode", { read: ViewContainerRef })
  inputMode: ViewContainerRef;
  inputModeComponent: ComponentRef<InputModeComponent>;
  // 在途模式
  @ViewChild("routeMode", { read: ViewContainerRef })
  routeMode: ViewContainerRef;
  routeModeComponent: ComponentRef<RouteModeComponent>;
  // 批次模式
  @ViewChild("batchMode", { read: ViewContainerRef })
  batchMode: ViewContainerRef;
  batchModeComponent: ComponentRef<BatchModeComponent>;
  // 提醒
  @ViewChild("remind", { read: ViewContainerRef })
  remind: ViewContainerRef;
  remindComponent: ComponentRef<RemindComponent>;
  constructor(
    public resolver: ComponentFactoryResolver,
    public recordService: RecordService
  ) { }

  ngOnInit() {
    this.showInputMode();
  }
  // 录入模式列表
  showInputMode() {
    // 判断组件是否已创建，如果创建先注销
    if (this.inputModeComponent && this.inputModeComponent.instance) {
      this.inputModeComponent.destroy();
    }
    // 动态创建组件的实例
    const inputModeComponent = this.resolver.resolveComponentFactory(InputModeComponent);
    this.inputModeComponent = this.inputMode.createComponent(inputModeComponent);
  }
  // 在途模式列表
  showRouteMode() {
    // 判断组件是否已创建，如果创建先注销
    if (this.routeModeComponent && this.routeModeComponent.instance) {
      this.routeModeComponent.destroy();
    }
    // 动态创建组件的实例
    const routeModeComponent = this.resolver.resolveComponentFactory(RouteModeComponent);
    this.routeModeComponent = this.routeMode.createComponent(routeModeComponent);
  }
  // 批次模式列表
  showBatchMode() {
    // 判断组件是否已创建，如果创建先注销
    if (this.batchModeComponent && this.batchModeComponent.instance) {
      this.batchModeComponent.destroy();
    }
    // 动态创建组件的实例
    const batchModeComponent = this.resolver.resolveComponentFactory(BatchModeComponent);
    this.batchModeComponent = this.batchMode.createComponent(batchModeComponent);
  }
  // 提醒
  showRemind() {
    // 判断组件是否已创建，如果创建先注销
    if (this.remindComponent && this.remindComponent.instance) {
      this.remindComponent.destroy();
    }
    // 动态创建组件的实例
    const remindComponent = this.resolver.resolveComponentFactory(RemindComponent);
    this.remindComponent = this.remind.createComponent(remindComponent);
  }
}
